package cn.zhangjie.org.service.impl;

import cn.zhangjie.basic.service.imp.BasicServiceImpl;
import cn.zhangjie.basic.util.Md5Utils;
import cn.zhangjie.basic.util.PageList;
import cn.zhangjie.basic.util.StrUtils;
import cn.zhangjie.org.domain.Employee;
import cn.zhangjie.org.mapper.EmployeeMapper;
import cn.zhangjie.org.query.EmployeeQuery;
import cn.zhangjie.org.service.IEmployeeService;
import cn.zhangjie.user.domain.Logininfo;
import cn.zhangjie.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
//查询的方法比较多，使用查询的事务配置
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
//@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BasicServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void add(Employee employee){

        //1.处理密码和盐值
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密之后的密码
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword()+salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);

        employee.setLogininfo_id(logininfo.getId());





        employeeMapper.save(employee);
        //保存中间表
        employeeMapper.saveEmoployeeRole(employee.getId(),employee.getRole_id());
    }

    @Override
    public void update(Employee employee) {
        //1.处理密码和盐值
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密之后的密码
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword()+salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        Logininfo logininfo = employee2Logininfo(employee);
        Long logininfo_id = employeeMapper.loadById(employee.getId()).getLogininfo_id();
        logininfo.setId(logininfo_id);

        logininfoMapper.update(logininfo);





        super.update(employee);
        employeeMapper.removeEmployeeRole(employee.getId());
        employeeMapper.saveEmoployeeRole(employee.getId(),employee.getRole_id());

    }
    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        return logininfo;
    }

    @Override
    public void del(Long id) {
        //先删除t_employee_role
        employeeMapper.removeEmployeeRole(id);
        employeeMapper.remove(id);
    }

    @Override
    public List<Employee> findByShopId(Long shopId) {

        return employeeMapper.findByShopId(shopId);
    }
}